package cn.itcast.search.controller;

import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.search.SearchReqDTO;
import cn.itcast.hmall.pojo.item.ItemDoc;
import cn.itcast.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author oldwine
 * @date 2022/6/22
 * @Version 1.0
 */
@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private SearchService searchService;

    /**
     * 搜索栏自动补全
     *
     * @param key 搜索词条
     * @return 查询结果
     */
    @GetMapping("suggestion")
    public List<String> getSuggestions(@RequestParam("key") String key) {
        return searchService.getSuggestions(key);
    }

    /**
     * 过滤项聚合功能
     * @param params 请求参数对象集合
     * @return 返回结果
     */
    @PostMapping("filters")
    public Map<String, List<String>> getFilters(@RequestBody SearchReqDTO params) {
        return searchService.getFilters(params);
    }

    /**
     * 搜索功能
     * @param params 请求参数对象
     * @return 结果
     */
    @PostMapping("list")
    public PageDTO<ItemDoc> list(@RequestBody SearchReqDTO params){
        return searchService.search(params);
    }
}
